/**
 * author levi
 * url http://levi.cg.am
 */
$(function() {
	$(document).mousemove(function(e) {
		if(!!this.move) {
			var posix = !document.move_target ? {
					'x': 0,
					'y': 0
				} : document.move_target.posix,
				callback = document.call_down || function() {
					$(this.move_target).css({
						'top': e.pageY - posix.y,
						'left': e.pageX - posix.x
					});
				};

			callback.call(this, e, posix);
		}
		e.stopPropagation();
	}).mouseup(function(e) {
		if(!!this.move) {
			var callback = document.call_up || function() {};
			callback.call(this, e);
			$.extend(this, {
				'move': false,
				'move_target': null,
				'call_down': false,
				'call_up': false
			});
		}
		e.stopPropagation();
	});

	var $box = $('#box').mousedown(function(e) {
		var offset = $(this).offset();

		this.posix = {
			'x': e.pageX - offset.left,
			'y': e.pageY - offset.top
		};
		$.extend(document, {
			'move': true,
			'move_target': this
		});
	}).on('mousedown', '#coor', function(e) {
		var posix = {
			'w': $box.width(),
			'h': $box.height(),
			'x': e.pageX,
			'y': e.pageY
		};

		$.extend(document, {
			'move': true,
			'call_down': function(e) {
				var width = Math.max(30, e.pageX - posix.x + posix.w);
				var height = Math.max(30, e.pageY - posix.y + posix.h);
				
				if(Math.max(30, e.pageX - posix.x + posix.w)<600){
					width = "600px";
				}
				if(Math.max(30, e.pageY - posix.y + posix.h)<630){
					height = "630px";
				}
				$box.css({
					'width': width,
					'height': height
				});
				$(".contentSection").height($box.height()-40);
			}
		});
		
		e.stopPropagation();
		return false;
	});
	var $noteBox = $('#noteBox');
		$("#coors").on('mousedown',function(e) {
		var posix = {
			'w': $noteBox.width(),
			'h': $noteBox.height(),
			'x': e.pageX,
			'y': e.pageY
		};

		$.extend(document, {
			'move': true,
			'call_down': function(e) {
				var width = Math.max(30, e.pageX - posix.x + posix.w);
				var height = Math.max(30, e.pageY - posix.y + posix.h);
                //
				if(Math.max(30, e.pageX - posix.x + posix.w)<504){
					width = "504px";
				}
				if(Math.max(30, e.pageY - posix.y + posix.h)<460){
					height = "460px";
				}
				$noteBox.css({
					'width': width,
					'height': height
				});
//				$(".contentSection").height($noteBox.height()-40);
			}
		});

		e.stopPropagation();
		return false;
	});
});